<?php
class AppController extends Controller {
	var $helpers = array('Html','Form','Session','Time','Text',"Link","Ajax");
	var $components = array('Uploader.Uploader','RequestHandler','Session','Cookie','Auth');
	var $uses = array('User','News','Infor','Support','Slide','Cateproduct','Product','Maker','Country');
   function beforeFilter(){
	parent::beforeFilter();
    $this->Auth->loginAction = array('admin' => true, 'controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('admin' =>true,'controller' => 'pages', 'action' => 'display');
    $this->Auth->loginError = 'Thông tin đăng nhập không đúng';
    $this->Auth->authError = 'Truy cập bị từ chối';
    $this->Auth->allow('admin_login','index','display','catenews','detail','view','search','login','home');
		if(isset($this->params['admin'])) {
			$this->layout = 'admin';
			if($this->Auth->user()) $this->set('ssid',$this->Auth->user());
			$this->_admin();
		}else{          
			$this->layout = 'default';      
      }
    $this->set('model',$this->uses[0]);
		
	}

   
	function _site(){ 
	  
    $hSlide = $this->Slide->find('all');
    $menu_products = $this->Cateproduct->find('list', array('conditions'=>array('status'=>2),'order'=>array('Cateproduct.order'=>'asc')));
		$menu_intros = $this->Intro->find('list', array('conditions'=>array('status'=>2),'order'=>array('Intro.order'=>'asc')));
    $menu_support = $this->Support->find('all', array('conditions'=>array('status'=>2)));
    //$sContact = $this->Infor->find('first', array('conditions'=>array('lang'=>$this->lg,'area'=>'contact'),'order'=>array('Infor.id'=>'DESC')));
    $sIntro = $this->Infor->find('first', array('conditions'=>array('lang'=>$this->lg,'area'=>'about'),'order'=>array('Infor.id'=>'DESC')));
    $sFooter = $this->Infor->find('first', array('conditions'=>array('lang'=>$this->lg,'area'=>'footer'),'order'=>array('Infor.id'=>'DESC')));
		$sSlogan = $this->Infor->find('first', array('conditions'=>array('lang'=>$this->lg,'area'=>'slogan'),'order'=>array('Infor.id'=>'DESC')));
		$this->set(compact('hSlide','sSlogan','hService','menu_products','menu_quangchau','menu_services','menu_intros','menu_support','sContact','sIntro','sFooter'));
	//debug($hSlide);
	}
	function _admin(){
		$idus=NULL;
		if($this->Auth->user()){
              $idus = $this->Auth->user('id');
          }
		$newsadmin = $this->News->find('list',array('limit'=>5,'order News.id'=>' DESC'));
		$inforlogin  = $this->News->query("SELECT * FROM lastlogin WHERE users_id = '".$idus."' limit 0,1");
		$product = $this->Product->find('list',array('fields'=>array('id','name'),'limit'=>5,'order Product.id'=>' DESC'));
		$this->set(compact('newsadmin','inforlogin','product'));
	}
}
function liststatus(){
	return array(1=>'Không hiển thị',2=>'Hiển thị');
}
function search($array, $key, $value)
	{
		$results = array();

		if (is_array($array))
		{
			if (isset($array[$key]) && $array[$key] == $value)
				$results[] = $array;

			foreach ($array as $subarray)
				$results = array_merge($results, search($subarray, $key, $value));
		}

		return $results;
	}
?>